
def compute_metrics_with_fuse(golds, preds):
    # 用一个矩阵储存结果 (3,3)
    # 3 class * (tp, fp, fn)
    # 结果顺序为 precision, recall, f1, tp, positive, true
    preds = [i for arr in preds for i in arr]
    golds = [i for arr in golds for i in arr]
    counts = [[0, 0, 0], [0, 0, 0]]
    label_name_list = ["DEL|FE", "DEL|EF"]
    
    for i, v in enumerate(golds):
        if golds[i] != "S-DEL|F" and golds[i] != "S-DEL|E":
            if preds[i] == "S-DEL|F":
                counts[0][1] += 1 #F-fp
            elif preds[i] == "S-DEL|E":
                counts[1][1] += 1 #E-fp
            else:
                pass
        elif golds[i] == 'S-DEL|F':
            if preds[i] == 'S-DEL|F':
                counts[0][0] += 1  #F-tp
            elif preds[i] == 'S-DEL|E':
                counts[0][0] += 1
            else:
                counts[0][2] += 1  #F-fp
        elif golds[i] == 'S-DEL|E':
            if preds[i] == 'S-DEL|E':
                counts[1][0] += 1  # E-tp
            elif preds[i] == 'S-DEL|F':
                counts[1][0] += 1
            else:
                counts[1][2] += 1
        else:
            pass

    report='{:>12s}{:>11s}{:>10s}{:>10s}{:>10s}{:>10s}{:>10s}\n'.format('','precision','recall','f1-score','tp','tp+fp','tp+fn')
    f1s = []
    for i, v in enumerate(counts):
        if (counts[i][0] + counts[i][1]) == 0:
            p = 0
        else:
            p = counts[i][0]/(counts[i][0] + counts[i][1])

        if (counts[i][0] + counts[i][2]) == 0:
            r = 0
        else:
            r = counts[i][0]/(counts[i][0] + counts[i][2])

        if (p + r) == 0:
            f1 = 0
        else:
            f1 = 2*p*r/(p+r)
        f1s.append(f1)
        report += '{:>12s}{:>11.4f}{:>10.4f}{:>10.4f}{:>10d}{:>10d}{:>10d}\n'.format(
            label_name_list[i], p, r, f1, counts[i][0], (counts[i][0] + counts[i][1]), (counts[i][0] + counts[i][2]))

    return report, f1s[1]



if __name__ == '__main__':
    y_true = [['S-DEL|E',  'S-DEL|E',  'S-INS|0', 'S-INS|0', 'S-INS|0', 'S-INS|0', 'S-INS|0', 'S-INS|0', 'S-INS|0', ]]
    y_pred = [['S-DEL|F',  'S-DEL|E',  'S-DEL|E', 'S-INS|0', 'S-INS|0', 'S-INS|0', 'S-INS|0', 'S-DEL|E', 'S-DEL|E',]]
    masks = [[1, ] * 9]
    print(compute_metrics_with_fuse(y_true, y_pred))